import {MidwareConfig} from "../midware";
import {stringUtils} from "../../utils/stringUtils";
import jQuery from 'jquery';

let coursewareConfigInit = false;

class CoursewareConfigManager {
    constructor() {
        if (!CoursewareConfigManager.instance) {
            this._coursewareConfigSet = {};

            CoursewareConfigManager.instance = this;
        }

        return CoursewareConfigManager.instance;
    }

    loadConfig(path = undefined, file = 'coursesubject.json', async = true) {
        if (coursewareConfigInit) {
            return;
        }
        const cPath = stringUtils.urlJoin(MidwareConfig.BasePath, path, file, '?releaseNum=' + MidwareConfig.PlayerReleaseNumber);
        return jQuery.ajax({
            url: cPath,
            dataType: 'json',
            async: async
        }).done(data => {
            this._coursewareConfigSet = data;
            coursewareConfigInit = true;
        }).fail(() => {
            console.warn('courseware object config load error!!');
        });
    }

    getConfig(key) {
        return this._coursewareConfigSet[key] || null;
    }
}

const coursewareConfigManager = new CoursewareConfigManager();
export default coursewareConfigManager;